package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.CoursesDao;
import dao.InfoManageDao;
import dao.SurveyContentDao;
import dao.SurveyDetailDao;
import dao.TeacherDao;
import entity.Courses;
import entity.InfoManage;
import entity.SurveyContent;
import entity.SurveyDetail;
import entity.Teacher;

/**

 * Servlet implementation class GetDetail

 */
@WebServlet("/manage/GetDetail") // 在manage文件夹里在前面加manage，不然ajax的url找不到

public class GetDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**

	 * @see HttpServlet#HttpServlet()

	 */
	public GetDetail() {
		super();
		// TODO Auto-generated constructor stub


	}

	/**

	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse

	 *      response)

	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		InfoManageDao imd = new InfoManageDao();
		List<Date> dates = imd.getDate();
		String newdate = request.getParameter("mdate");
		if (newdate == null || newdate.equals("")) {
			newdate = dates.get(0).toString(); // 设置时间为最新的日期

		}
		List<InfoManage> list = imd.getByDate(newdate);
		TeacherDao tea = new TeacherDao();
		List<Teacher> teachers = tea.getAll();
		CoursesDao coud = new CoursesDao();
		SurveyDetailDao sdd=new SurveyDetailDao();
		List<SurveyDetail> list_Detail = null;
		SurveyContentDao scd=new SurveyContentDao();
		SurveyContent sc=new SurveyContent(); 

	
		String contentName="";
		String sd = "{\"teachers\":[";
		for (InfoManage s : list) {
			for (Teacher t : teachers) {
				if (t.getId() == s.getTeacher_Id()) {
					t.getIms().add(s);				
					System.out.println("session_id:"+s.getSession_id());
				}
			}
		}

		for (Teacher t : teachers) {
//			System.out.println(t.getName() + ":");


			sd += "{\"teacherName\":\"" + t.getName() + "\"";
			List<InfoManage> tims = t.getIms();
			Courses cou=new Courses();
			String coursesName="";
//			System.out.println("tims.size()"+tims.size());


		

			if(tims.size()>0)
			{
				cou.setId(tims.get(0).getCourses_Id());
//				System.out.println(tims.get(0));	


//				System.out.println("coursesid:"+tims.get(0).getCourses_Id());


				coursesName=coud.getOne(cou).getName();
				
			}
			sd += ",\"coursesName\":\""+coursesName+"\",\"items\":[";
			double avg=0;
			int score_Man=tims.size();
	
			for (InfoManage i : tims) {
				avg+=i.getScore_Num();
				if(i.getSession_id()==null||i.getSession_id().equals("")){
					i.setSession_id("没有学生编号");
				  };
				sd += "{\"imid\":" + i.getId() + ",\"score_Num\":" + i.getScore_Num()+",\"session_Id\":\""+i.getSession_id()+"\",\"suggest\":\""+i.getSuggest()+"\"";
				
				sd +=",\"details\":["; 
				list_Detail=sdd.getScoreAndContent(i.getId());
				 for (SurveyDetail detail : list_Detail) {
					  sc.setId(detail.getContent_Id());
					  contentName=scd.getOne(sc).getContent();
					  sd+="{\"contentName\":\""+contentName+"\",\"score_One\":"+detail.getScore()+"},";
				} 
				 if(list_Detail.size()>0)
					{
					 sd = sd.substring(0, sd.length() - 1);
					}
				 sd +="]},";
//				list_infoid.add(i.getId());


//				list_Detail=sdd.getScoreAndContent(i.getId());


			}
			if(tims.size()>0)
				{
					sd = sd.substring(0, sd.length() - 1);
					avg/=tims.size();
				}
			sd += "],\"avg\":"+avg+",\"score_Man\":"+score_Man+"},";
		}
		sd = sd.substring(0, sd.length() - 1);
		
		sd +="],\"dates\":[";
		for (Date dt : dates) {
			sd += "{\"date\":\"" + dt + "\"},";
		}
		sd = sd.substring(0, sd.length() - 1);
		sd += "]}";
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.print(sd); // 把InfoManage表里面的数据输出到管理员界面上

		System.out.println(sd);
	}

	/**

	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse

	 *      response)

	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub


		doGet(request, response);
	}

}
